package com.wlf.admin.system.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.http.HtmlUtil;
import com.wlf.admin.common.dto.Const;
import com.wlf.admin.common.dto.ParamPage;
import com.wlf.admin.common.util.AjaxBean;
import com.wlf.admin.common.util.JSON;
import com.wlf.admin.system.domain.SysUser;
import com.wlf.admin.system.service.SysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private SysUserService userService;

    @GetMapping("/main")
    ModelAndView indexHtml() {
        return new ModelAndView("user/index");
    }

    @PostMapping("/list")
    AjaxBean list(SysUser user, ParamPage paramPage) {
        return userService.getList(user, paramPage);
    }

    @PostMapping("/add")
    AjaxBean add(SysUser user) {
        return userService.add(user);
    }

    @GetMapping("/addForm")
    ModelAndView addFormHtml() {
        ModelAndView view = new ModelAndView("user/add");
        view.addObject("userStatusList", ListUtil.of(Const.UserStatus.values()));
        view.addObject("userStatus", Const.UserStatus.normal.name());
        view.addObject("formData", JSON.toJSONString(userService.formData()));
        return view;
    }

    @GetMapping("/editForm")
    ModelAndView editFormHtml(Long id) {
        ModelAndView view = new ModelAndView("user/edit");
        view.addObject("userStatusList", ListUtil.of(Const.UserStatus.values()));
        view.addObject("user", userService.get(id));
        view.addObject("formData", JSON.toJSONString(userService.formData()));
        return view;
    }


    @PostMapping("/update")
    AjaxBean update(SysUser user) {
        return userService.update(user);
    }

    @GetMapping("/del")
    AjaxBean del(Long id) {
        return userService.del(id);
    }

    @PostMapping("/delAll")
    AjaxBean delAll(String ids) {
        return userService.delAll(JSON.parseArray(HtmlUtil.unescape(ids), Long.class));
    }

    @GetMapping("/reset")
    AjaxBean reset(Long id) {
        return userService.reset(id);
    }
}
